package cn.iocoder.yudao.module.iot.controller.admin.warnpolicy;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.iot.controller.admin.warnpolicy.vo.*;
import cn.iocoder.yudao.module.iot.dal.dataobject.warnpolicy.WarnPolicyDO;
import cn.iocoder.yudao.module.iot.service.warnpolicy.WarnPolicyService;

@Tag(name = "管理后台 - 设备消防策略")
@RestController
@RequestMapping("/iot/warnPolicy")
@Validated
public class WarnPolicyController {

    @Resource
    private WarnPolicyService warnPolicyService;

    @PostMapping("/create")
    @Operation(summary = "创建设备消防策略")
    @PreAuthorize("@ss.hasPermission('iot:warn-policy:create')")
    public CommonResult<Long> createWarnPolicy(@Valid @RequestBody WarnPolicySaveReqVO createReqVO) {
        return success(warnPolicyService.createWarnPolicy(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备消防策略")
    @PreAuthorize("@ss.hasPermission('iot:warn-policy:update')")
    public CommonResult<Boolean> updateWarnPolicy(@Valid @RequestBody WarnPolicySaveReqVO updateReqVO) {
        warnPolicyService.updateWarnPolicy(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备消防策略")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('iot:warn-policy:delete')")
    public CommonResult<Boolean> deleteWarnPolicy(@RequestParam("id") Long id) {
        warnPolicyService.deleteWarnPolicy(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备消防策略")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:warn-policy:query')")
    public CommonResult<WarnPolicyRespVO> getWarnPolicy(@RequestParam("id") Long id) {
        WarnPolicyDO warnPolicy = warnPolicyService.getWarnPolicy(id);
        return success(BeanUtils.toBean(warnPolicy, WarnPolicyRespVO.class));
    }

    @PostMapping("/page")
    @Operation(summary = "获得设备消防策略分页")
    //@PreAuthorize("@ss.hasPermission('iot:warn-policy:query')")
    public CommonResult<PageResult<WarnPolicyRespVO>> getWarnPolicyPage(@Valid WarnPolicyPageReqVO pageReqVO) {
        PageResult<WarnPolicyDO> pageResult = warnPolicyService.getWarnPolicyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WarnPolicyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备消防策略 Excel")
    @PreAuthorize("@ss.hasPermission('iot:warn-policy:export')")
    @OperateLog(type = EXPORT)
    public void exportWarnPolicyExcel(@Valid WarnPolicyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WarnPolicyDO> list = warnPolicyService.getWarnPolicyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "设备消防策略.xls", "数据", WarnPolicyRespVO.class,
                        BeanUtils.toBean(list, WarnPolicyRespVO.class));
    }

}